package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.CommentQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelCommentService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;
    @Autowired
    private ITravelCommentService travelCommentService;

    @PostMapping
    public JsonResult save(Travel travel) {
        JsonResult result = new JsonResult();
        try {
            //关联当前用户
            travel.setAuthor(UserContext.getCurrentUser());
            travelService.save(travel);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @PutMapping
    public JsonResult update(Travel travel) {
        JsonResult result = new JsonResult();
        try {
            //关联当前用户
            travelService.update(travel);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @GetMapping("{id}")
    public Travel get(@PathVariable Long id) {

        return travelService.get(id);
    }

    /**
     * 查询所有已发布的游记
     *
     * @param qo
     * @return
     */
    @GetMapping
    public PageInfo<Travel> listAllReleaseTravel(TravelQueryObject qo) {
        //设置已发布状态的高级查询条件
        qo.setState(2);
        qo.setOrderBy("t.lastUpdateTime DESC");
        return travelService.queryForList(qo);
    }

    //浏览点击数量
    @PutMapping("/updateBrowseNum")
    public JsonResult updateBrowseNum(Travel travel)
    {
        JsonResult result = new JsonResult();
        try {
            //关联当前用户
            travelService.updateBrowseNum(travel);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }


    /**
     * 查询指定游记下的所有评论信息
     *
     * @param qo
     * @return
     */
    @GetMapping("/{travelId}/comments")
    public PageInfo<Strategy> listCommonds(CommentQueryObject qo) {
        PageInfo pageInfo = travelCommentService.query(qo);
        return pageInfo;
    }


}
